<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title-container">
        <h2 class="title">掘金后台管理系统</h2>
        <p class="subtitle">欢迎登录</p>
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            ref="usernameRef"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            ref="passwordRef"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            size="large"
            clearable
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
            <template #suffix>
              <el-icon class="password-icon" @click="showPwd">
                <View v-if="passwordType === 'password'" />
                <Hide v-else />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button 
            :loading="loading" 
            type="primary" 
            size="large"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="demo-tips">
        <el-alert
          title="演示账号"
          type="info"
          :closable="false"
          show-icon
        >
          <template #default>
            <div>用户名：<strong>zhangsan</strong></div>
            <div>密码：<strong>123456789</strong></div>
          </template>
        </el-alert>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginForm = reactive({
  username: 'zhangsan',
  password: '123456789'
})

const loginRules = {
  username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
  password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
}

const passwordType = ref('password')
const loading = ref(false)
const loginFormRef = ref(null)
const usernameRef = ref(null)
const passwordRef = ref(null)

const showPwd = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
  nextTick(() => {
    passwordRef.value?.focus()
  })
}

const handleLogin = () => {
  if (!loginFormRef.value) return
  
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login(loginForm)
        router.push({ path: route.query.redirect || '/', replace: true })
        ElMessage.success('登录成功')
      } catch (error) {
        ElMessage.error('登录失败: ' + error.message)
      } finally {
        loading.value = false
      }
    } else {
      ElMessage.warning('请填写完整的登录信息')
      return false
    }
  })
}

onMounted(() => {
  if (loginForm.username === '') {
    usernameRef.value?.focus()
  } else if (loginForm.password === '') {
    passwordRef.value?.focus()
  }
})
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.title-container {
  text-align: center;
  margin-bottom: 40px;

  .title {
    font-size: 28px;
    font-weight: 600;
    color: #2c3e50;
    margin: 0 0 8px 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }

  .subtitle {
    font-size: 16px;
    color: #7f8c8d;
    margin: 0;
    font-weight: 400;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 24px;

    :deep(.el-input) {
      .el-input__wrapper {
        background-color: #f8f9fa;
        border: 2px solid #e9ecef;
        border-radius: 12px;
        box-shadow: none;
        transition: all 0.3s ease;
        padding: 8px 16px;

        &:hover {
          border-color: #667eea;
        }

        &.is-focus {
          border-color: #667eea;
          box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
      }

      .el-input__inner {
        color: #2c3e50;
        font-size: 16px;
        font-weight: 400;

        &::placeholder {
          color: #95a5a6;
          font-weight: 400;
        }
      }

      .el-input__prefix,
      .el-input__suffix {
        color: #7f8c8d;
      }
    }
  }

  .password-icon {
    cursor: pointer;
    transition: color 0.3s ease;

    &:hover {
      color: #667eea;
    }
  }

  .login-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 12px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

.demo-tips {
  margin-top: 24px;

  :deep(.el-alert) {
    border-radius: 12px;
    border: none;
    background-color: #e8f4fd;

    .el-alert__content {
      .el-alert__title {
        color: #2c3e50;
        font-weight: 600;
        margin-bottom: 8px;
      }

      .el-alert__description {
        color: #5a6c7d;
        line-height: 1.6;

        div {
          margin-bottom: 4px;

          &:last-child {
            margin-bottom: 0;
          }

          strong {
            color: #667eea;
            font-weight: 600;
          }
        }
      }
    }

    .el-alert__icon {
      color: #667eea;
    }
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    margin: 20px;
    padding: 30px 24px;
  }

  .title-container {
    margin-bottom: 30px;

    .title {
      font-size: 24px;
    }

    .subtitle {
      font-size: 14px;
    }
  }
}
</style>